import { defineStore } from 'pinia'
import { ref } from 'vue'
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'

export const useUserStore = defineStore('user', {
  state: () => ({
    token: '',
    userInfo: {
      username: '',
      avatar: '',
      roles: []
    }
  }),
  actions: {
    setToken(newToken: string) {
      this.token = newToken
    },
    clearToken() {
      this.token = ''
    },
    setUserInfo(info: any) {
      this.userInfo = info
    },
    async login(username: string, password: string) {
      return new Promise((resolve) => {
        setTimeout(() => {
          const token = 'mock-token-' + Date.now()
          this.setToken(token)
          this.setUserInfo({
            username,
            avatar: 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png',
            roles: ['admin']
          })
          resolve({ success: true })
        }, 300)
      })
    },
    logout() {
      this.clearToken()
      this.userInfo = {
        username: '',
        avatar: '',
        roles: []
      }
    }
  },
  persist: true
}) 